<?php
namespace uploader;
use qiniu\OssQiniu;
use qcloud\OssQCloud;
use alioss\OssAli;
use think\facade\Env;
use think\facade\Config;

class uploader {

    /**
     * 错误变量
     * @var
     */
    public $errorCode = EC_SUCCESS;

    /***
     * @desc 将本地图片上传到远程服务器
     * @param $fileName
     * @param $pathName
     * @param $flag
     * @param $oss
     * @param $cdn
     * @return mixed|string
     */
    public function uploadToOss($fileName, $pathName, $flag = false, $oss = 'ALL',$cdn = 'YES') {
        if($cdn == 'YES'){
            if(empty(Env::get('oss.url'))){
                $this->errorCode = EC_ENV_CONFIG_ERROR;
                return false;
            }
        }
        if ($oss == 'ALL'){
            $url = [];
            $url[] = $this->uploadToOss($fileName, $pathName, $flag, 'ALI', $cdn);
            $url[] = $this->uploadToOss($fileName, $pathName, $flag, 'QINIU', $cdn);
            $url[] = $this->uploadToOss($fileName, $pathName, $flag, 'QCLOUD', $cdn);

            if ($cdn == 'YES'){
                for ($i = 0 ; $i < count($url) ; $i++){
                    if (!empty($url[$i]))return $url[$i];
                }
            }
            return output_format($url);
        }elseif($oss == 'ALI'){
            $oss = $this->ToAli();
        }elseif ($oss == 'QINIU') {
            $oss = $this->ToQiniu();
        }elseif ($oss == 'QCLOUD') {
            $oss = $this->ToQCloud();
        }
        return $oss->uploadImage($fileName, $pathName, $flag, $cdn);
    }

    private function ToAli(){
        return new OssAli();
    }

    private function ToQiniu(){
        return new OssQiniu();
    }

    private function ToQCloud(){
        return new OssQCloud();
    }
}